package com.rongshu.sendmessage.view.bottomView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.rongshu.sendmessage.R;
import com.rongshu.sendmessage.adapter.CatogryAdapter;
import com.rongshu.sendmessage.bean.CatogryInfoBean;
import com.rongshu.sendmessage.util.ClientPublicParamUtil;
import com.rongshu.sendmessage.util.DialogUtil;
import com.rongshu.sendmessage.util.httpUtil.HYContent;
import com.rongshu.sendmessage.util.itemDecoration.MyItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CatogryBottomDialog implements View.OnClickListener {
    private final DialogUtil.OnDialogEvent<CatogryInfoBean> callBack;
    private final Context context;
    private final CatogryInfoBean locationBean;
    private View inflate;
    private Context mContext;
    private BottomSheetDialog bsd1;
    private RecyclerView recycleView;
    private List<CatogryInfoBean> mList;
    private View ib_colse;
    private CatogryAdapter mAdapter;


    public CatogryBottomDialog(Context context,CatogryInfoBean locationBean, DialogUtil.OnDialogEvent<CatogryInfoBean> callBack){
        this.callBack=callBack;
        this.context=context;
        this.locationBean=locationBean;
    }
    public View showDialog() {
        //自定义dialog显示布局
        inflate = LayoutInflater.from(context).inflate(R.layout.catogry_bottom_view, null);
        mContext=context;
        initView(inflate);
        //自定义dialog显示风格
        bsd1=new BottomSheetDialog(mContext);
        inflate.post(new Runnable() {
            @Override
            public void run() {
                BottomSheetBehavior behavior= BottomSheetBehavior.from(bsd1.findViewById(R.id.design_bottom_sheet));
                behavior.setHideable(false);//此处设置表示禁止BottomSheetBehavior的执行
                behavior.setPeekHeight(600);
            }
        });
        bsd1.setContentView(inflate);
        bsd1.getWindow().findViewById(R.id.design_bottom_sheet)
                .setBackgroundResource(android.R.color.transparent);
        bsd1.setCanceledOnTouchOutside(false);
//        bsd1.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

        bsd1.show();
        bsd1.setCanceledOnTouchOutside(false);
        initData("");
        return inflate;
    }
    public boolean isShow(){
        return bsd1.isShowing();
    }
    private void initData(String parentId) {
        DialogUtil.showProgress(mContext);
        //银行列表//
        Map<String,String> params=new HashMap<>();
        params.put("parentId",parentId);
        params.put("clientPublicParam", ClientPublicParamUtil.getPublicParams(mContext));
        OkGo.<String>post(new HYContent().CATOGRY_LIST_URL).params(params).tag("CATOGRY_LIST_URL").
                execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        mList.clear();
                        DialogUtil.hideProgress();
                        try {
                            List<CatogryInfoBean> list = JSON.parseArray(response.body(), CatogryInfoBean.class);
                            if (locationBean!=null)
                                for (int i = 0; i < list.size(); i++) {
                                    if (list.get(i).getTypeid().equals(locationBean.getTypeid())){
                                        list.get(i).setSelect(true);
                                    }else {
                                        list.get(i).setSelect(false);
                                    }
                                }
                            mList.addAll(list);
                            mAdapter.notifyDataSetChanged();
                        } catch (Exception e) {
                            e.printStackTrace();
                            DialogUtil.hideProgress();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        DialogUtil.hideProgress();

                    }
                });
    }

    private void initView(View inflate) {
        ib_colse=inflate.findViewById(R.id.ib_colse);

        ib_colse.setOnClickListener(this);
        recycleView=inflate.findViewById(R.id.recycleView);
//        listView.setLayoutAnimation(AnimationUtil.getAnimationController());
        GridLayoutManager manager = new GridLayoutManager(mContext, 2);
        manager.setOrientation(GridLayoutManager.VERTICAL);
        recycleView.setLayoutManager(manager);
        //把自定义的ItemDecoration设置给RecyclerView
        recycleView.addItemDecoration(new MyItemDecoration(10,10));
        //设置设置RecyclerView的适配器Adapter
        mList = new ArrayList<>();
        mAdapter = new CatogryAdapter(mContext,mList);
        recycleView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(position -> {
            callBack.onPositiveButtonClick(mList.get(position));
            bsd1.cancel();

        });
//        mList=new ArrayList<>();
//        mAdapter=new CatogryOneAdapter(mContext,mList);
//        listView.setAdapter(mAdapter);
//        listView.setOnItemClickListener((parent, view, position, id) -> {
//            callBack.onPositiveButtonClick(mList.get(position));
//            bsd1.cancel();
//        });
    }
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.ib_colse) {
            bsd1.cancel();
        }
    }
}
